package com.kehutong.payfees.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.coraframework.inject.Inject;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

import com.kehutong.payfees.entity.LawyerEvidenceFile;

@WebService("/charge/lawyer/evidence")
public class LawyerEvidenceFileServiceImpl {

    @Inject
    private JdbcSession sqlSession;

    public Object getLawyerEvidence(String id) {
        List<LawyerEvidenceFile> lawyerEvidenceFiles = sqlSession.findArray(LawyerEvidenceFile.class)
                .eq("chargeMediationId", id)
                .eq("deleted", false).exe();
        Map<String, List<LawyerEvidenceFile>> resultMap = new HashMap<>();
        lawyerEvidenceFiles.stream().collect(Collectors.groupingBy(LawyerEvidenceFile::getEvidenceType)).forEach((k, v) -> {
            resultMap.put(k.getId(), v);
        });
        return resultMap;
    }

    public void saveLawyerEvidence(LawyerEvidenceFile lawyerEvidenceFile) {
        sqlSession.insert(lawyerEvidenceFile);
    }

    public void deleteLawyerEvidence(String id) {
        LawyerEvidenceFile lawyerEvidenceFile = Pool.get(LawyerEvidenceFile.class, id);
        lawyerEvidenceFile.setDeleted(true);
        sqlSession.updateById(lawyerEvidenceFile);
    }
}
